<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/29/0029
 * Time: 15:56
 */

namespace app\sports_admin\model;


use think\Model;

class Backups extends Model
{
    protected $table = 'backups';
    protected $pk = 'id';
    protected $autoWriteTimestamp = true;
    public function getCreateTimeAttr($value)
    {
        return date("Y-m-d H:i:s",$value);
    }
    /**
     * 获取备份列表
     * @param $page 第几页
     * @param $rows 每页显示的条数
     * @param $sort 要排序的字段
     * @param $sortOrder 升序/降序
     * @param $searchName 要检索的字段
     * @param $start 备份的开始时间
     * @param $end 备份的结束时间
    */
    public static function getBackupsList($page,$rows,$sort,$sortOrder,$searchName,$start,$end)
    {
        $result = self::where(function($query)use($searchName,$start,$end){
            if($searchName){
                $query->where('backup_name','like',"%{$searchName}%");
            }
            if($start && $end){
                $query->whereTime('create_time', 'between', ["{$start}", "{$end}"]);
            }
        })->order("{$sort}","{$sortOrder}")->page($page)->limit($rows)->select();
        $count = self::where(function($query)use($searchName,$start,$end){
            if($searchName){
                $query->where('backup_name','like',"%{$searchName}%");
            }
            if($start && $end){
                $query->whereTime('create_time', 'between', ["{$start}", "{$end}"]);
            }
        })->count();
        return json(['rows'=>$result,'total'=>$count]);
    }
}